local hx__jiefa = fk.CreateSkill {
  name = "hx__jiefa",
}

hx__jiefa:addEffect("active", {
  name = "hx__jiefa",
  card_num = 0,
  target_num = 0,
  prompt = "#hx__jiefa",
  can_use = function(self, player)
    return player:usedSkillTimes(hx__jiefa.name, Player.HistoryPhase) == 0 and player:getMark("@hx__jiefa_guaxiang") > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:removePlayerMark(player, "@hx__jiefa_guaxiang", 1)
    local choice2 = {"移去一枚「卦象」然后声明一种牌的类型并进行一次判定"}
    if player:getMark("@hx__jiefa_guaxiang") > 1 then
      table.insert(choice2, "移去两枚「卦象」标记令你本回合使用【杀】无视距离且不可被响应")
    end
    local choose = room:askToChoice(player, {
      choices = choice2,
      skill_name = hx__jiefa.name,
    })
    if choose == "移去一枚「卦象」然后声明一种牌的类型并进行一次判定" then
      local choice = room:askToChoice(player, {
        choices = {"basic", "trick", "equip"},
        skill_name = hx__jiefa.name,
      })
      local ty
      if choice == "basic" then
        ty = Card.TypeBasic
      elseif choice == "trick" then
        ty = Card.TypeTrick
      else
        ty = Card.TypeEquip
      end
      room:sendLog{
        type = "%from “%arg2” 声明了类型 %arg",
        from = player.id,
        arg = choice,
        arg2 = hx__jiefa.name,
        toast = true,
      }
      local judge = {
        who = player,
        reason = hx__jiefa.name,
        pattern = ".|.",
      }
      room:judge(judge)
      if judge.card.type == ty then
        player:drawCards(2, hx__jiefa.name)
        room:recover{
          who = player,
          num = 1,
          skillName = hx__jiefa.name,
          recoverBy = player,
        }
      else
        local tos = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player),
          min_num = 1,
          max_num = 1,
          prompt = "请选择一名其他角色对其造成X点雷电伤害（X为该角色已损失体力值且至少为1",
          skill_name = hx__jiefa.name,
          cancelable = false,
        })
        local to = tos[1]
        local x = math.max(to.maxHp - to.hp, 1)
        room:damage{
          from = player,
          to = to,
          damage = x,
          damageType = fk.ThunderDamage,
          skillName = hx__jiefa.name,
        }
      end
    elseif choose == "移去两枚「卦象」标记令你本回合使用【杀】无视距离且不可被响应" then
      room:addPlayerMark(player, "@@hx__jiefa_sha-turn")
    end
  end,
})

hx__jiefa:addEffect(fk.RoundStart, {
  anim_type = "support",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__jiefa.name) and player:getMark("@hx__jiefa_guaxiang") <= 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@hx__jiefa_guaxiang", 3)
  end,
})
hx__jiefa:addEffect(fk.GameStart, {
  anim_type = "support",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__jiefa.name) and player:getMark("@hx__jiefa_guaxiang") <= 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@hx__jiefa_guaxiang", 3)
  end,
})

hx__jiefa:addEffect("targetmod", {
  name = "#hx__jiefa_dis",
  bypass_distances =  function(self, player, skill, card, to)
    return player:hasSkill(hx__jiefa.name) and skill.trueName == "slash_skill" and player:getMark("@@hx__jiefa_sha-turn") > 0
  end,
})

hx__jiefa:addEffect(fk.CardUsing, {
  name = "#hx__jiefa_sha",
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@hx__jiefa_sha-turn") > 0 and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
  end,
})

return hx__jiefa